/*************************************************************************
	> File Name: oj-358.cpp
	> Author: Wang Bo
	> Mail: 1276106221@qq.com
	> Created Time: 2021年02月04日 星期四 23时51分52秒
 ************************************************************************/
#include <iostream>
using namespace std;

// 区间DP
#define MAX_N 300
#define INF 0x3f3f3f3f
// 状态定义
// dp[i][j] 合并i~j区间石子需要的体力
// dp[i][j] = min(dp[i][k] + dp[k][j]) | k ∈ [i, j]

int dp[MAX_N + 5][MAX_N + 5], val[MAX_N + 5], n;
int main() {
    cin >> n;
    for (int i = 1; i <= n; ++i) cin >> val[i], val[i] += val[i - 1]; //存成前缀和
      //遍历长度Len
    for (int len = 2; len <= n; ++len) {
        for (int i = 1; i <= n - len + 1; ++i) {
            int j = i + len - 1; //右端点
            dp[i][j] = INF;
            //调整k的位置，计算区间体力最优
            for (int k = i; k <= j; ++k) {
                dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 1][j] + val[j] - val[i - 1]);
            }
        }
    }
    cout << dp[1][n] << endl;
    return 0;
}
